package GUI;

import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import ControlLayer.ProductCtr;
import ControlLayer.SupplierCtr;
import ModelLayer.Product;


public class ProductMangement implements ItemListener {

	private JFrame frame;
	private JPanel findPanel;
	private JPanel gearAmount;
	private JTextPane productPane3;
	private JTextPane findpane;
	private JTextField txtserialNumber;
	private JTextField txtproductName;
	private JTextField txtpurchasePrice;
	private JTextField txtsalesPrice;
	private JTextField txtrentPrice;
	private JTextField txtcountryOfOrigin;
	private JTextField txtFabricant;
	private JTextField txtDescription;
	private JTextField txtcarModel;
	private JTextField txtminStock;
	private JTextField txtmaxStock;
	private JTextField txtmodelYear;
	private JTextField txtproductID;
	private JTextField txtgearboxType;
	private JTextField txttextField_1;
	private JTextField txttireType;
	private JTextField txttireSize;
	private JTextField txtglassWidth;
	private JTextField txtglassTickness;
	private JTextField txtbrakediscType;
	private JTextField txtLightsType;
	private JTextField txtexProductId;
	private JTextField txtsuppliertext;
	private JTextField txtglassHeight;
	private JLabel lblexProductId;
	private JLabel lblGearboxType;
	private JLabel lblGearAmount;
	private JLabel lblTireType;
	private JLabel lblTireSize;
	private JLabel lblGlassHeight;
	private JLabel lblLightsType;
	private JLabel lblGlassTickness;
	private JLabel lblGlassWidth;
	private JLabel lblBrakediscType;
	private JTable tblProducts;
	private ProductCtr productctr;
	private SupplierCtr supplierctr;
	private ProductTable producttable;
	private JComboBox comboBox;
	private String productType;
	private int supplierId;
	private JButton btnReturn;

	/**
	 * Create the application.
	 */
	public ProductMangement() {
		makeFrame();
		makePanels();
		frame.setVisible(true);
	}

	/**
	 * Initialize the contents of the frame.
	 */

	private void makeFrame()
	{
		frame = new JFrame();
		frame.setBounds(100, 100, 887, 491);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.getContentPane().setLayout(null);
	}
	
	/**
	 * Initialize the Text Fields.
	 */
	private void makeTextFields()
	{
		txtexProductId = new JTextField();
		txtexProductId.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				productPane3.setText("Enter Valid Product Number");
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				clearPane();
			}
		});
		txtexProductId.setBounds(355, 400, 124, 20);
		gearAmount.add(txtexProductId);
		txtexProductId.setColumns(10);
		txtexProductId.setVisible(false);
		
		txtproductID = new JTextField();
		txtproductID.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				int key = e.getKeyCode();
				if (key == KeyEvent.VK_ENTER) {
					searchMethod();
				}
			}
		});
		txtproductID.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent arg0) {
				clearPane2();
				txtproductID.setText("");
			}
		});
		txtproductID.setBounds(305, 10, 253, 20);
		findPanel.add(txtproductID);
		txtproductID.setColumns(10);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 36, 850, 354);
		findPanel.add(scrollPane);
		
		tblProducts = new JTable();
		tblProducts.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		scrollPane.setViewportView(tblProducts);
		producttable = new ProductTable();
		tblProducts.setModel(producttable);
		
		JButton btnNewButton_3 = new JButton("List all Products");
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				listallproductsmethod();
			}
		});
		btnNewButton_3.setBounds(712, 9, 148, 23);
		findPanel.add(btnNewButton_3);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Runtime Info:", TitledBorder.LEADING, TitledBorder.TOP, null, Color.RED));
		panel_1.setBounds(2, 0, 173, 35);
		findPanel.add(panel_1);
		panel_1.setLayout(null);
		
		findpane = new JTextPane();
		findpane.setEditable(false);
		findpane.setBounds(11, 13, 156, 16);
		panel_1.add(findpane);
		findpane.setBackground(SystemColor.control);
		findpane.setForeground(SystemColor.textHighlight);
		
		btnReturn = new JButton("Return");
		btnReturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				closeMethod();
			}
		});
		btnReturn.setBounds(10, 399, 89, 23);
		findPanel.add(btnReturn);
		
		txtserialNumber = new JTextField();
		txtserialNumber.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				productPane3.setText("Enter Valid Serial Number");
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				clearPane();
			}
		});
		txtserialNumber.setBounds(157, 56, 124, 20);
		gearAmount.add(txtserialNumber);
		txtserialNumber.setColumns(10);
		
		txtproductName = new JTextField();
		txtproductName.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				productPane3.setText("Enter Product Name");
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				clearPane();
			}
		});
		txtproductName.setColumns(10);
		txtproductName.setBounds(157, 87, 124, 20);
		gearAmount.add(txtproductName);
		
		txtpurchasePrice = new JTextField();
		txtpurchasePrice.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				productPane3.setText("Enter Purchase Price");
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				clearPane();
			}
		});
		txtpurchasePrice.setColumns(10);
		txtpurchasePrice.setBounds(157, 118, 124, 20);
		gearAmount.add(txtpurchasePrice);
		
		txtsalesPrice = new JTextField();
		txtsalesPrice.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				productPane3.setText("Enter Sales Price");
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				clearPane();
			}
		});
		txtsalesPrice.setColumns(10);
		txtsalesPrice.setBounds(157, 149, 124, 20);
		gearAmount.add(txtsalesPrice);
		
		txtrentPrice = new JTextField();
		txtrentPrice.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				productPane3.setText("Enter Rent Price");
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				clearPane();
			}
		});
		txtrentPrice.setColumns(10);
		txtrentPrice.setBounds(157, 180, 124, 20);
		gearAmount.add(txtrentPrice);
		
		txtcountryOfOrigin = new JTextField();
		txtcountryOfOrigin.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				productPane3.setText("Enter COR (Country of Origin)");
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				clearPane();
			}
		});
		txtcountryOfOrigin.setColumns(10);
		txtcountryOfOrigin.setBounds(157, 211, 124, 20);
		gearAmount.add(txtcountryOfOrigin);
		
		txtFabricant = new JTextField();
		txtFabricant.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				productPane3.setText("Enter Fabricant");
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				clearPane();
			}
		});
		txtFabricant.setColumns(10);
		txtFabricant.setBounds(157, 242, 124, 20);
		gearAmount.add(txtFabricant);
		
		txtDescription = new JTextField();
		txtDescription.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				productPane3.setText("Enter Product Description (max 250 char)");
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				clearPane();
			}
		});
		txtDescription.setColumns(10);
		txtDescription.setBounds(157, 273, 124, 20);
		gearAmount.add(txtDescription);
		
		txtcarModel = new JTextField();
		txtcarModel.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				productPane3.setText("Enter Car Model");
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				clearPane();
			}
		});
		txtcarModel.setColumns(10);
		txtcarModel.setBounds(157, 304, 124, 20);
		gearAmount.add(txtcarModel);
		
		txtminStock = new JTextField();
		txtminStock.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				productPane3.setText("Enter Minimum Stock");
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				clearPane();
			}
		});
		txtminStock.setColumns(10);
		txtminStock.setBounds(458, 57, 124, 20);
		gearAmount.add(txtminStock);
		
		txtmaxStock = new JTextField();
		txtmaxStock.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				productPane3.setText("Enter Maximum Stock");
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				clearPane();
			}
		});
		txtmaxStock.setColumns(10);
		txtmaxStock.setBounds(458, 88, 124, 20);
		gearAmount.add(txtmaxStock);
		
		txtmodelYear = new JTextField();
		txtmodelYear.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				productPane3.setText("Enter Car Model Year");
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				clearPane();
			}
		});
		txtmodelYear.setColumns(10);
		txtmodelYear.setBounds(157, 335, 124, 20);
		gearAmount.add(txtmodelYear);
		
		txtgearboxType = new JTextField();
		txtgearboxType.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				productPane3.setText("Enter Gearbox Type");
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				clearPane();
			}
		});
		txtgearboxType.setColumns(10);
		txtgearboxType.setBounds(733, 57, 124, 20);
		gearAmount.add(txtgearboxType);
		txtgearboxType.setVisible(false);
		
		txttextField_1 = new JTextField();
		txttextField_1.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				productPane3.setText("Enter Gear Amount");
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				clearPane();
			}
		});
		txttextField_1.setColumns(10);
		txttextField_1.setBounds(733, 88, 124, 20);
		gearAmount.add(txttextField_1);
		txttextField_1.setVisible(false);
		
		JPanel GearBox = new JPanel();
		GearBox.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Special Attributes ", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GearBox.setBounds(630, 39, 235, 113);
		gearAmount.add(GearBox);
		GearBox.setLayout(null);
		
		lblGlassHeight = new JLabel("Glass Height");
		lblGlassHeight.setBounds(6, 21, 87, 14);
		GearBox.add(lblGlassHeight);
		lblGlassHeight.setVisible(false);
		
		txtglassHeight = new JTextField();
		txtglassHeight.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				productPane3.setText("Enter Glass Height");
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				clearPane();
			}
		});
		txtglassHeight.setColumns(10);
		txtglassHeight.setBounds(103, 18, 124, 20);
		GearBox.add(txtglassHeight);
		txtglassHeight.setVisible(false);
		
		lblLightsType = new JLabel("Lights Type");
		lblLightsType.setBounds(6, 21, 87, 14);
		GearBox.add(lblLightsType);
		lblLightsType.setVisible(false);
		
		lblGlassWidth = new JLabel("Glass Width");
		lblGlassWidth.setBounds(6, 52, 87, 14);
		GearBox.add(lblGlassWidth);
		lblGlassWidth.setVisible(false);
		
		lblGlassTickness = new JLabel("Glass Tickness");
		lblGlassTickness.setBounds(6, 82, 87, 14);
		GearBox.add(lblGlassTickness);
		lblGlassTickness.setVisible(false);
		
		lblBrakediscType = new JLabel("Brakedisc Type");
		lblBrakediscType.setBounds(6, 21, 113, 14);
		GearBox.add(lblBrakediscType);
		lblBrakediscType.setVisible(false);
		
		txtglassTickness = new JTextField();
		txtglassTickness.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				productPane3.setText("Enter Glass Tickness");
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				clearPane();
			}
		});
		txtglassTickness.setBounds(103, 78, 124, 20);
		GearBox.add(txtglassTickness);
		txtglassTickness.setColumns(10);
		txtglassTickness.setVisible(false);
		
		txtbrakediscType = new JTextField();
		txtbrakediscType.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				productPane3.setText("Enter Brakedisc Type");
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				clearPane();
			}
		});
		txtbrakediscType.setBounds(103, 18, 124, 20);
		GearBox.add(txtbrakediscType);
		txtbrakediscType.setColumns(10);
		txtbrakediscType.setVisible(false);
		
		txtLightsType = new JTextField();
		txtLightsType.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				productPane3.setText("Enter Lights Type");
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				clearPane();
			}
		});
		txtLightsType.setBounds(103, 18, 124, 20);
		GearBox.add(txtLightsType);
		txtLightsType.setColumns(10);
		txtLightsType.setVisible(false);
		
		txtglassWidth = new JTextField();
		txtglassWidth.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				productPane3.setText("Enter Glass Width");
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				clearPane();
			}
		});
		txtglassWidth.setBounds(103, 49, 124, 20);
		GearBox.add(txtglassWidth);
		txtglassWidth.setColumns(10);
		
		lblTireType = new JLabel("Tire Type");
		lblTireType.setBounds(6, 21, 87, 14);
		GearBox.add(lblTireType);
		
		lblGearboxType = new JLabel("Gearbox Type");
		lblGearboxType.setBounds(6, 21, 87, 14);
		GearBox.add(lblGearboxType);
		
		lblGearAmount = new JLabel("Gear Amount");
		lblGearAmount.setBounds(6, 52, 87, 14);
		GearBox.add(lblGearAmount);
		
		lblTireSize = new JLabel("Tire Size");
		lblTireSize.setBounds(6, 52, 87, 14);
		GearBox.add(lblTireSize);
		
		txttireType = new JTextField();
		txttireType.setBounds(103, 18, 124, 20);
		GearBox.add(txttireType);
		txttireType.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				productPane3.setText("Enter Tire Type");
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				clearPane();
			}
		});
		txttireType.setColumns(10);
		
		txttireSize = new JTextField();
		txttireSize.setBounds(103, 49, 124, 20);
		GearBox.add(txttireSize);
		txttireSize.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				productPane3.setText("Enter Tire Size");
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				clearPane();
			}
		});
		txttireSize.setColumns(10);
		txttireSize.setVisible(false);
		txttireType.setVisible(false);
		lblTireSize.setVisible(false);
		lblGearAmount.setVisible(false);
		lblGearboxType.setVisible(false);
		lblTireType.setVisible(false);
		txtglassWidth.setVisible(false);
		
		
		txtsuppliertext = new JTextField();
		txtsuppliertext.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				productPane3.setText("Enter Valid Supplier Phone Number");
				txtsuppliertext.setText("");
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				clearPane();
				supplierctr = new SupplierCtr();
				String supplier = supplierctr.getSupplier(Integer.parseInt(txtsuppliertext.getText()));
				supplierId = Integer.parseInt(txtsuppliertext.getText());
				if(supplier != null){
					txtsuppliertext.setText(supplier);
					txtsuppliertext.setEnabled(false);
				}else if(supplier == null)
				{
					productPane3.setText("Not A Valid Supplier");
					txtsuppliertext.setText("No Supplier Found");
				}
			}
		});
		txtsuppliertext.setColumns(10);
		txtsuppliertext.setBounds(458, 119, 124, 20);
		gearAmount.add(txtsuppliertext);
		
		JButton btnNewButton_2 = new JButton("Return");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				closeMethod();
			}
		});
		btnNewButton_2.setBounds(10, 399, 89, 23);
		gearAmount.add(btnNewButton_2);
		
		comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"Gearbox", "Tire", "Glass", "Brakedisc", "Lights"}));
		comboBox.setBounds(458, 150, 124, 20);
		comboBox.addItemListener(this);
		gearAmount.add(comboBox);
		
		JPanel panel = new JPanel();
		panel.setForeground(Color.BLACK);
		panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Runtime Info:", TitledBorder.LEADING, TitledBorder.TOP, null, Color.RED));
		panel.setBounds(327, 180, 255, 45);
		gearAmount.add(panel);
		panel.setLayout(null);
		
		productPane3 = new JTextPane();
		productPane3.setEditable(false);
		productPane3.setBounds(9, 17, 233, 20);
		panel.add(productPane3);
		productPane3.setBackground(SystemColor.control);
		productPane3.setForeground(SystemColor.textHighlight);
		
	}
	
	/**
	 * Initialize the Buttons.
	 */
	private void makeButtons()
	{
		
		JButton btnNewButton_1 = new JButton("Confirm Update");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateProductMethod();
			}
		});
		btnNewButton_1.setBounds(565, 399, 141, 23);
		gearAmount.add(btnNewButton_1);
		
		final JCheckBox CB1 = new JCheckBox("Update existing product");			
		CB1.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				onCheckBox1();
			}
		});
		CB1.setBounds(565, 369, 201, 23);
		gearAmount.add(CB1);
		
		JButton buttonCancel = new JButton("Search");
		buttonCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				searchMethod();
			}
		});
		buttonCancel.setBounds(564, 9, 103, 23);
		findPanel.add(buttonCancel);
		
		JButton btnNewButton = new JButton("Delete Given Product");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				deleteMethod();
			}
		});
		btnNewButton.setBounds(688, 399, 172, 23);
		findPanel.add(btnNewButton);
		
		JButton buttonConfirmReturn = new JButton("Confirm Insert");
		buttonConfirmReturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				addProductMethod();
			}
		});
		buttonConfirmReturn.setBounds(716, 399, 141, 23);
		gearAmount.add(buttonConfirmReturn);
	}
	
	/**
	 * Initialize the panels.
	 */
	private void makePanels()
	{
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(0, 0, 875, 461);
		frame.getContentPane().add(tabbedPane);
		
		gearAmount = new JPanel();
		tabbedPane.addTab("Insert / Update Product", null, gearAmount, null);
		gearAmount.setLayout(null);
		
		findPanel = new JPanel();
		tabbedPane.addTab("Find Product", null, findPanel, null);
		findPanel.setLayout(null);
		
		makeLabels();
		makeButtons();
		makeTextFields();
		
	}
	
	/**
	 * Initialize the labels.
	 */
	private void makeLabels()
	{
		JLabel lblSupplierphone = new JLabel("Supplier (Phone)");
		lblSupplierphone.setBounds(327, 122, 131, 14);
		gearAmount.add(lblSupplierphone);
		
		JLabel lblRentPrice = new JLabel("Rent Price");
		lblRentPrice.setBounds(10, 180, 157, 14);
		gearAmount.add(lblRentPrice);
		
		JLabel lblCountryOfOrigin = new JLabel("Country Of Origin");
		lblCountryOfOrigin.setBounds(10, 211, 178, 14);
		gearAmount.add(lblCountryOfOrigin);
		
		JLabel lblFabricant = new JLabel("Fabricant");
		lblFabricant.setBounds(10, 242, 157, 14);
		gearAmount.add(lblFabricant);
		
		JLabel lblDescription = new JLabel("Description");
		lblDescription.setBounds(10, 273, 178, 14);
		gearAmount.add(lblDescription);
		
		JLabel lblCarModel = new JLabel("Car Model");
		lblCarModel.setBounds(10, 304, 137, 14);
		gearAmount.add(lblCarModel);
		
		JLabel lblModelYear = new JLabel("Model Year");
		lblModelYear.setBounds(10, 335, 157, 14);
		gearAmount.add(lblModelYear);
		
		JLabel lblMinimumStock = new JLabel("Minimum Stock");
		lblMinimumStock.setBounds(327, 57, 105, 14);
		gearAmount.add(lblMinimumStock);
		
		JLabel lblMaximumStock = new JLabel("Maximum Stock");
		lblMaximumStock.setBounds(327, 88, 121, 14);
		gearAmount.add(lblMaximumStock);
		
		JLabel lblProductType = new JLabel("Product Type");
		lblProductType.setBounds(327, 153, 87, 14);
		gearAmount.add(lblProductType);
		
		JLabel lblSerialNumber = new JLabel("Serial Number");
		lblSerialNumber.setBounds(10, 56, 137, 14);
		gearAmount.add(lblSerialNumber);
		
		JLabel lblDateReturn = new JLabel("Purchase Price");
		lblDateReturn.setBounds(10, 118, 157, 14);
		gearAmount.add(lblDateReturn);
		
		JLabel lblOrderIdReturn = new JLabel("Product Name");
		lblOrderIdReturn.setBounds(10, 87, 137, 14);
		gearAmount.add(lblOrderIdReturn);
		
		JLabel lblProductReturnId = new JLabel("Insert Product Values");
		lblProductReturnId.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblProductReturnId.setBounds(10, 11, 157, 14);
		gearAmount.add(lblProductReturnId);
		
		JLabel lblProductId = new JLabel("Product Number:");
		lblProductId.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblProductId.setBounds(179, 11, 122, 14);
		findPanel.add(lblProductId);
		
		JLabel lblAmountReturn = new JLabel("Sales Price");
		lblAmountReturn.setBounds(10, 149, 137, 14);
		gearAmount.add(lblAmountReturn);
		
		lblexProductId = new JLabel("Enter Excisting Product ID");
		lblexProductId.setBounds(157, 403, 183, 14);
		gearAmount.add(lblexProductId);
		lblexProductId.setVisible(false);
	}
	
	/**
	 * Initialize method which works with the checkbox.
	 */
	public void onCheckBox1()
	{
		if(lblexProductId.isVisible() && txtexProductId.isVisible() == true)
		{
			lblexProductId.setVisible(false);
			txtexProductId.setVisible(false);
		}else{
			lblexProductId.setVisible(true);
			txtexProductId.setVisible(true);
		}
	}
	
	/**
	 * Initialize search method where product ID is entered.
	 */
	public void searchMethod()
	{
		productctr = new ProductCtr();
		String pro = txtproductID.getText();
		if(pro.isEmpty()){
			findpane.setText("No Product ID Entered");
		}else if(pro.equals("Gearbox") || pro.equals("gearbox"))
			{
				listallGearbox();
			}else if(pro.equals("Tire") || pro.equals("tire"))
			{
				listallTire();
			}else if(pro.equals("Glass") || pro.equals("glass"))
			{
				listallGlass();
			}else if(pro.equals("Brakedisc") || pro.equals("brakedisc"))
			{
				listallBrakedisc();
			}else if(pro.equals("Headlight") || pro.equals("headlight"))
			{
				listallHeadlight();
			}else if(pro.equals("*") || pro.equals(""))
			{
				listallproductsmethod();
			}
		else{
			int proID = Integer.parseInt(txtproductID.getText());
			ArrayList<Product> producta = new ArrayList<Product>();
			producta.add(productctr.findProduct(proID, false));
			if(producta != null){
				ProductTable proModel = new ProductTable();
				proModel.setProducts(producta);
				tblProducts.setModel(proModel);
				frame.setVisible(true);
				findpane.setText("Selected PN: " + txtproductID.getText());
			}
		}
		}

	
	/**
	 * Lists all products in database.
	 */
	public void listallproductsmethod()
	{
		productctr = new ProductCtr();
		producttable.setProducts(productctr.listAllProducts());
		findpane.setText("All Products Listed");
	}
	
	/**
	 * Delete at product with givin ID.
	 */
	public void deleteMethod()
	{
		String pro = txtproductID.getText();
		productctr = new ProductCtr();
		if(pro.isEmpty()){
			findpane.setText("No Product ID Entered");
		}else{
		int proID = Integer.parseInt(txtproductID.getText());
		productctr.deleteProduct(proID);
		listallproductsmethod();
		findpane.setText("Product Deleted");
		}
	}
	
	/**
	 * Adds a new product in database.
	 */
	public void addProductMethod()
	{
		productctr = new ProductCtr();
		int serialNumber = Integer.parseInt(txtserialNumber.getText());
		String productName = txtproductName.getText();
		double purchasePrice = Integer.parseInt(txtpurchasePrice.getText());
		double salesPrice = Integer.parseInt(txtsalesPrice.getText());
		double rentPrice = Integer.parseInt(txtrentPrice.getText());
		String countryOfOrigin = txtcountryOfOrigin.getText();
		String fabricant = txtFabricant.getText();
		String description = txtDescription.getText(); 
		String carModel = txtcarModel.getText();
		int modelYear = Integer.parseInt(txtmodelYear.getText());
		int minStock = Integer.parseInt(txtminStock.getText());
		int maxStock = Integer.parseInt(txtmaxStock.getText());
		
		if(productType.equals("Gearbox"))
		{
			String gearboxType = txtgearboxType.getText();
			int gearAmount = Integer.parseInt(txttextField_1.getText());
			productctr.insertGearbox(serialNumber, productName, supplierId, purchasePrice, salesPrice, rentPrice, countryOfOrigin, fabricant, description, carModel, modelYear, minStock, maxStock, productType, gearboxType, gearAmount);
			productPane3.setText("Product Inserted Correctly");
		}
		
		if(productType.equals("Tire"))
		{
			String tireType = txttireType.getText();
			String tireSize = txttireSize.getText();
			productctr.insertTire(serialNumber, productName, supplierId, purchasePrice, salesPrice, rentPrice, countryOfOrigin, fabricant, description, carModel, modelYear, minStock, maxStock, productType, tireType, tireSize);
			productPane3.setText("Product Inserted Correctly");
		}
		
		if(productType.equals("Glass"));
		{
			String height = txtglassHeight.getText();
			String width = txtglassWidth.getText();
			String thickness = txtglassTickness.getText();
			productctr.insertGlass(serialNumber, productName, supplierId, purchasePrice, salesPrice, rentPrice, countryOfOrigin, fabricant, description, carModel, modelYear, minStock, maxStock, productType, height, width, thickness);
			productPane3.setText("Product Inserted Correctly");
		}
		
		if(productType.equals("Brakedisc"));
		{
			String discType = txtbrakediscType.getText();
			productctr.insertBrakeDisc(serialNumber, productName, supplierId, purchasePrice, salesPrice, rentPrice, countryOfOrigin, fabricant, description, carModel, modelYear, minStock, maxStock, productType, discType);
			productPane3.setText("Product Inserted Correctly");
		}
		
		if(productType.equals("Headlight"));
		{
			String lightsType = txtLightsType.getText();
			productctr.insertHeadlights(serialNumber, productName, supplierId, purchasePrice, salesPrice, rentPrice, countryOfOrigin, fabricant, description, carModel, modelYear, minStock, maxStock, productType, lightsType);
			productPane3.setText("Product Inserted Correctly");
		}
	}
	
	/**
	 * Updates a product.
	 */
	public void updateProductMethod()
	{
		productctr = new ProductCtr();
		int productNumber = Integer.parseInt(txtexProductId.getText());
		int serialNumber = Integer.parseInt(txtserialNumber.getText());
		String productName = txtproductName.getText();
		double purchasePrice = Integer.parseInt(txtpurchasePrice.getText());
		double salesPrice = Integer.parseInt(txtsalesPrice.getText());
		double rentPrice = Integer.parseInt(txtrentPrice.getText());
		String countryOfOrigin = txtcountryOfOrigin.getText();
		String fabricant = txtFabricant.getText();
		String description = txtDescription.getText(); 
		String carModel = txtcarModel.getText();
		int modelYear = Integer.parseInt(txtmodelYear.getText());
		int minStock = Integer.parseInt(txtminStock.getText());
		int maxStock = Integer.parseInt(txtmaxStock.getText());
		
		if(productType.equals("Gearbox"))
		{
			String gearboxType = txtgearboxType.getText();
			int gearAmount = Integer.parseInt(txttextField_1.getText());
			productctr.updateGearbox(productNumber, serialNumber, productName, supplierId, purchasePrice, salesPrice, rentPrice, countryOfOrigin, fabricant, description, carModel, modelYear, minStock, maxStock, productType, gearboxType, gearAmount);
			productPane3.setText("Product Updated Correctly");
		}
		
		if(productType.equals("Tire"))
		{
			String tireType = txttireType.getText();
			String tireSize = txttireSize.getText();
			productctr.updateTire(productNumber, serialNumber, productName, supplierId, purchasePrice, salesPrice, rentPrice, countryOfOrigin, fabricant, description, carModel, modelYear, minStock, maxStock, productType, tireType, tireSize);
			productPane3.setText("Product Updated Correctly");
		}
		
		if(productType.equals("Glass"))
		{
			String height = txtglassHeight.getText();
			String width = txtglassWidth.getText();
			String thickness = txtglassTickness.getText();
			productctr.updateGlass(productNumber, serialNumber, productName, supplierId, purchasePrice, salesPrice, rentPrice, countryOfOrigin, fabricant, description, carModel, modelYear, minStock, maxStock, productType, height, width, thickness);
			productPane3.setText("Product Updated Correctly");
		}
		
		if(productType.equals("Brakedisc"))
		{
			String discType = txtbrakediscType.getText();
			productctr.updateBrakeDisc(productNumber, serialNumber, productName, supplierId, purchasePrice, salesPrice, rentPrice, countryOfOrigin, fabricant, description, carModel, modelYear, minStock, maxStock, productType, discType);
			productPane3.setText("Product Updated Correctly");
		}
		
		if(productType.equals("Headlight"))
		{
			String lightsType = txtLightsType.getText();
			productctr.updateHeadlights(productNumber, serialNumber, productName, supplierId, purchasePrice, salesPrice, rentPrice, countryOfOrigin, fabricant, description, carModel, modelYear, minStock, maxStock, productType, lightsType);
			productPane3.setText("Product Updated Correctly");
		}
	}
	
	/**
	 * Clears the pane on the first frame.
	 */
	private void clearPane()
	{
		productPane3.setText("");
	}
	
	/**
	 * Clears the pane on the second frame.
	 */
	private void clearPane2()
	{
		findpane.setText("");
	}
	
	/**
	 * Close the frame.
	 */
	private void closeMethod()
	{
		frame.dispose();
	}
	
	/**
	 * Listing all gearbox
	 */
	private void listallGearbox()
	{
		productctr = new ProductCtr();
		producttable.setGearbox(productctr.listAllGearbox());
	}
	
	/**
	 * Listing all gearbox
	 */
	private void listallTire()
	{
		productctr = new ProductCtr();
		producttable.setTire(productctr.listAllTire());
	}

	/**
	 * Listing all glass
	 */
	private void listallGlass()
	{
		productctr = new ProductCtr();
		producttable.setGlass(productctr.listAllGlass());
	}

	/**
	 * Listing all brakedisc
	 */
	private void listallBrakedisc()
	{
		productctr = new ProductCtr();
		producttable.setBrakeDisc(productctr.listAllBrakeDisc());
	}

	/**
	 * Listing all lights
	 */
	private void listallHeadlight()
	{
		productctr = new ProductCtr();
		producttable.setHeadligth(productctr.listAllHeadlights());
	}
	
	/**
	 * Method used for selecting in the combo box.
	 */
	public void itemStateChanged(ItemEvent e) {
	    int Selection;
	    Selection = comboBox.getSelectedIndex();
	    if (Selection == 0) {
	    	txtgearboxType.setVisible(true);
	    	txttextField_1.setVisible(true);
	    	txttireType.setVisible(false);
	    	txttireSize.setVisible(false);
	    	txtglassHeight.setVisible(false);
	    	txtglassWidth.setVisible(false);
	    	txtglassTickness.setVisible(false);
	    	txtbrakediscType.setVisible(false);
	    	txtLightsType.setVisible(false);
	    	txtLightsType.setEnabled(false);
	    	txtgearboxType.setEnabled(true);
	    	txttextField_1.setEnabled(true);
	    	txttireType.setEnabled(false);
	    	txttireSize.setEnabled(false);
	    	txtglassHeight.setEnabled(false);
	    	txtglassWidth.setEnabled(false);
	    	txtglassTickness.setEnabled(false);
	    	txtbrakediscType.setEnabled(false);
	    	txtLightsType.setEnabled(false);
	    	lblGearboxType.setVisible(true);
			lblGearAmount.setVisible(true);
			lblTireType.setVisible(false);
			lblTireSize.setVisible(false);
			lblGlassHeight.setVisible(false);
			lblGlassTickness.setVisible(false);
			lblGlassWidth.setVisible(false);
			lblLightsType.setVisible(false);
			lblBrakediscType.setVisible(false);
	    	productType = "Gearbox";
	    	productPane3.setText("Gearbox Chosen");
	    } else if (Selection == 1) {
	    	txtgearboxType.setVisible(false);
	    	txttextField_1.setVisible(false);
	    	txttireType.setVisible(true);
	    	txttireSize.setVisible(true);
	    	txtglassHeight.setVisible(false);
	    	txtglassWidth.setVisible(false);
	    	txtglassTickness.setVisible(false);
	    	txtbrakediscType.setVisible(false);
	    	txtLightsType.setVisible(false);
	    	txtLightsType.setEnabled(false);
	    	txtgearboxType.setEnabled(false);
	    	txttextField_1.setEnabled(false);
	    	txttireType.setEnabled(true);
	    	txttireSize.setEnabled(true);
	    	txtglassHeight.setEnabled(false);
	    	txtglassWidth.setEnabled(false);
	    	txtglassTickness.setEnabled(false);
	    	txtbrakediscType.setEnabled(false);
	    	txtLightsType.setEnabled(false);
	    	lblGearboxType.setVisible(false);
			lblGearAmount.setVisible(false);
			lblTireType.setVisible(true);
			lblTireSize.setVisible(true);
			lblGlassHeight.setVisible(false);
			lblGlassTickness.setVisible(false);
			lblGlassWidth.setVisible(false);
			lblLightsType.setVisible(false);
			lblBrakediscType.setVisible(false);
	    	productType = "Tire";
	    	productPane3.setText("Tire Chosen");
	    } else if (Selection == 2) {
	    	txtgearboxType.setVisible(false);
	    	txttextField_1.setVisible(false);
	    	txttireType.setVisible(false);
	    	txttireSize.setVisible(false);
	    	txtglassHeight.setVisible(true);
	    	txtglassWidth.setVisible(true);
	    	txtglassTickness.setVisible(true);
	    	txtbrakediscType.setVisible(false);
	    	txtLightsType.setVisible(false);
	    	txtLightsType.setEnabled(false);
	    	txtgearboxType.setEnabled(false);
	    	txttextField_1.setEnabled(false);
	    	txttireType.setEnabled(false);
	    	txttireSize.setEnabled(false);
	    	txtglassHeight.setEnabled(true);
	    	txtglassWidth.setEnabled(true);
	    	txtglassTickness.setEnabled(true);
	    	txtbrakediscType.setEnabled(false);
	    	txtLightsType.setEnabled(false);
	    	lblGearboxType.setVisible(false);
			lblGearAmount.setVisible(false);
			lblTireType.setVisible(false);
			lblTireSize.setVisible(false);
			lblGlassHeight.setVisible(true);
			lblGlassTickness.setVisible(true);
			lblGlassWidth.setVisible(true);
			lblLightsType.setVisible(false);
			lblBrakediscType.setVisible(false);
	    	productType = "Glass";
	    	productPane3.setText("Glass Chosen");
	    } else if (Selection == 3) {
	    	txtgearboxType.setVisible(false);
	    	txttextField_1.setVisible(false);
	    	txttireType.setVisible(false);
	    	txttireSize.setVisible(false);
	    	txtglassHeight.setVisible(false);
	    	txtglassWidth.setVisible(false);
	    	txtglassTickness.setVisible(false);
	    	txtbrakediscType.setVisible(true);
	    	txtLightsType.setVisible(false);
	    	txtLightsType.setEnabled(false);
	    	txtgearboxType.setEnabled(false);
	    	txttextField_1.setEnabled(false);
	    	txttireType.setEnabled(false);
	    	txttireSize.setEnabled(false);
	    	txtglassHeight.setEnabled(false);
	    	txtglassWidth.setEnabled(false);
	    	txtglassTickness.setEnabled(false);
	    	txtbrakediscType.setEnabled(true);
	    	txtLightsType.setEnabled(false);
	    	lblGearboxType.setVisible(false);
			lblGearAmount.setVisible(false);
			lblTireType.setVisible(false);
			lblTireSize.setVisible(false);
			lblGlassHeight.setVisible(false);
			lblGlassTickness.setVisible(false);
			lblGlassWidth.setVisible(false);
			lblLightsType.setVisible(false);
			lblBrakediscType.setVisible(true);
	    	productType = "Brakedisc";
	    	productPane3.setText("Brakedisc Chosen");
	    } else if (Selection == 4) {
	    	txtgearboxType.setVisible(false);
	    	txttextField_1.setVisible(false);
	    	txttireType.setVisible(false);
	    	txttireSize.setVisible(false);
	    	txtglassHeight.setVisible(false);
	    	txtglassWidth.setVisible(false);
	    	txtglassTickness.setVisible(false);
	    	txtbrakediscType.setVisible(false);
	    	txtLightsType.setVisible(true);
	    	txtgearboxType.setEnabled(false);
	    	txttextField_1.setEnabled(false);
	    	txttireType.setEnabled(false);
	    	txttireSize.setEnabled(false);
	    	txtglassHeight.setEnabled(false);
	    	txtglassWidth.setEnabled(false);
	    	txtglassTickness.setEnabled(false);
	    	txtbrakediscType.setEnabled(false);
	    	txtLightsType.setEnabled(true);
	    	lblGearboxType.setVisible(false);
			lblGearAmount.setVisible(false);
			lblTireType.setVisible(false);
			lblTireSize.setVisible(false);
			lblGlassHeight.setVisible(false);
			lblGlassTickness.setVisible(false);
			lblGlassWidth.setVisible(false);
			lblLightsType.setVisible(true);
			lblBrakediscType.setVisible(false);
	    	productType = "Headlight";
	    	productPane3.setText("Headlight Chosen");
	  } 
	}
}
